package com.ruoyi.app.service.impl;

import cn.hutool.core.lang.Assert;
import com.alipay.api.AlipayApiException;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ijpay.alipay.AliPayApi;
import com.ruoyi.app.dao.MemberPackageMapper;
import com.ruoyi.app.entity.MemberPackage;
import com.ruoyi.app.entity.TempOrder;
import com.ruoyi.app.model.vo.PackageListRes;
import com.ruoyi.app.model.vo.RechargeReq;
import com.ruoyi.app.service.PayService;
import com.ruoyi.common.context.ApiUserContext;
import com.ruoyi.common.utils.JwtTokenUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.security.auth.message.AuthException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class PayServiceImpl implements PayService {


    @Autowired
    private MemberPackageMapper memberPackageMapper;


    @Override
    public List<PackageListRes> packageList() {

        LambdaQueryWrapper<MemberPackage> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(MemberPackage::getMoney);
        List<MemberPackage> memberPackageList = memberPackageMapper.selectList(queryWrapper);

        List<PackageListRes> resList = memberPackageList.stream().map(memberPackage -> {
            PackageListRes res = new PackageListRes();
            BeanUtils.copyProperties(memberPackage, res);
            return res;
        }).collect(Collectors.toList());

        return resList;
    }


    @Override
    public String recharge(RechargeReq req) {

        MemberPackage memberPackage = memberPackageMapper.selectById(req.getId());
        Assert.notNull(memberPackage, "套餐不存在！");
        double money = memberPackage.getMoney();

        if ("0".equals(req.getType())) {
            throw new IllegalArgumentException("暂不支持微信");
        }

        String orderId = UUID.randomUUID().toString().replaceAll("-", "");
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        model.setTotalAmount(String.valueOf(money));
        model.setSubject("会员充值");
        model.setOutTradeNo(orderId);
        //支付超时时间 10分钟
        model.setTimeoutExpress("10m");

        AlipayTradeAppPayResponse response = null;
        try {
            response = AliPayApi.appPayToResponse(model, "http://101.200.232.158/api/pay/ali_pay/notify_url");
            System.out.println(response.getBody());
        } catch (AlipayApiException e) {
            throw new IllegalArgumentException("支付宝调用异常！" + e.getMessage());
        }

        // 保存临时订单
        String userId = ApiUserContext.getLocalUser().getUserId();

        TempOrder tempOrder = new TempOrder();
        tempOrder.setOrderId(orderId);
        tempOrder.setUserId(userId);
        tempOrder.setPackageId(memberPackage.getId());
        tempOrder.setMoney(money);
        tempOrder.setPayType(req.getType());
        tempOrder.setHandle(0);
        tempOrder.setTimeoutExpress(LocalDateTime.now().plusMinutes(10));
        tempOrder.insert();

        return response.getBody();
    }


}
